package user

import (
	"context"
	"github.com/gin-gonic/gin"
	grpcuser "go-gin-api/app/proto/user"
	"go-gin-api/app/request"
	"go-gin-api/app/util/response"
	"net/http"
)
// user 用户
type User struct {
	userClient grpcuser.UserService
}

func NewUser(userClient grpcuser.UserService) *User {
	return &User{
		userClient: userClient,
	}
}

// 用户新增 Add
func (t *User) Add(c *gin.Context) {
	var (
		userRequest grpcuser.UserRequest
		req request.User
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1,err.Error(), nil)
		return
	}
	userRequest.Username = req.Username
	userRequest.Mobile = int32(req.Mobile)
	userRequest.Password = req.Password
	userRequest.Status = int32(req.Status)
	res, err := t.userClient.User(context.Background(), &userRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1, err.Error(), nil)
		return
	}
	if !res.Res {
		response.ReturnJson(c, http.StatusBadRequest,-1, "error", nil)
		return
	}
	response.ReturnJson(c, http.StatusOK,1, "success", nil)
}

// 用户编辑 Edit
func (t *User) Edit(c *gin.Context) {
	var (
		userUpdateRequest grpcuser.UserUpdateRequest
		req request.UserUpdate
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1,err.Error(), nil)
		return
	}
	userUpdateRequest.Id = req.Id
	userUpdateRequest.Username = req.Username
	userUpdateRequest.Mobile = int32(req.Mobile)
	userUpdateRequest.Password = req.Password
	userUpdateRequest.Status = int32(req.Status)
	res, err := t.userClient.UserUpdate(context.Background(), &userUpdateRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1, err.Error(), nil)
		return
	}
	if !res.Res {
		response.ReturnJson(c, http.StatusBadRequest,-1, "error", nil)
		return
	}
	response.ReturnJson(c, http.StatusOK,1, "success", nil)
}

// 用户删除 Delete
func (t *User) Delete(c *gin.Context) {
	var (
		UserDelRequest grpcuser.UserDelRequest
		req request.UserDel
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1,err.Error(), nil)
		return
	}
	UserDelRequest.Id = req.Id
	res, err := t.userClient.UserDel(context.Background(), &UserDelRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1, err.Error(), nil)
		return
	}
	if !res.Res {
		response.ReturnJson(c, http.StatusBadRequest,-1, "error", nil)
		return
	}
	response.ReturnJson(c, http.StatusOK,1, "success", nil)
}

// 用户详情 Info
func (t *User) Info(c *gin.Context) {
	var (
		UserInfoRequest grpcuser.UserInfoRequest
		req request.UserInfo
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1,err.Error(), nil)
		return
	}
	UserInfoRequest.Id = req.Id
	res, err := t.userClient.UserInfo(context.Background(), &UserInfoRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1, err.Error(), nil)
		return
	}
	response.ReturnJson(c, http.StatusOK,1, "success", res)
}


// 用户列表 List
func (t *User) List(c *gin.Context) {
	var (
		UserListRequest grpcuser.UserListRequest
		req request.UserList
	)
	// 参数绑定
	if err := c.ShouldBindJSON(&req); err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1,err.Error(), nil)
		return
	}
	UserListRequest.Status = req.Status
	UserListRequest.Limit = req.Limit
	UserListRequest.Page = req.Page
	UserListRequest.Username = req.Username
	UserListRequest.CreatedAt = req.CreatedAt
	res, err := t.userClient.UserList(context.Background(), &UserListRequest)
	if err != nil {
		response.ReturnJson(c, http.StatusBadRequest,-1, err.Error(), nil)
		return
	}
	response.ReturnJson(c, http.StatusOK,1, "success", res)
}